package juc.c_012;/*
   Talk is cheap. Show me the code.
 */

import java.util.ArrayList;

/**
 * @Description volatile并不能代替synchronized
 * @Date 2021/6/6
 * @Author liudl
 */
public class TestVolatile {
    private volatile int count=0;

    public /*synchronized*/ void m(){
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    public static void main(String[] args) {
        TestVolatile testVolatile = new TestVolatile();
        ArrayList<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            threads.add(new Thread(testVolatile::m, "t"+i));
        }
        threads.forEach(m->{
            m.start();
        });
        threads.forEach(o-> {
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(testVolatile.count);
    }
}
